<?php
/**
 * 店舗　共通コントローラクラス<br />
 *
 * @package apamanshop
 * @author
 * @copyright
 */
abstract class Shop_BaseController extends Apm_Controller_Base
{
    /** 表示テンプレートパス */
    protected $_display_path = null;
    /** Block */
    protected $_block = null;
    /** URL(ドメイン) */
    protected $_baseurl = null;
    /** URL(apl) */
    protected $_aplbaseurl = null;
    /** イメージURL */
    protected $_imageurl = null;
    /** 店舗イメージURL */
    protected $_tenpoimageurl = null;
    /** アパログURL */
    protected $_apalogurl = null;
    /** GoogleMapキー */
    protected $_googleMapKey = null;

    /** メインヘルパー */
    protected $_main_helper = null;
    /** メニューヘルパー */
    protected $_menu_helper = null;
    /** エラーヘルパー */
    protected $_error_helper = null;
    /** 店舗検索ヘルパー */
    protected $_tellistMenu_helper = null;
	/** 店舗検索結果ヘルパー */
    protected $_tenpoResult_helper = null;
    /** 新規オープン店舗ヘルパー */
	protected $_newopen_helper = null;
	/** 店舗検索（都道府県検索）ヘルパー */
    protected $_selectPref_helper = null;
    /** 店舗検索（沿線検索）ヘルパー */
    protected $_selectRail_helper = null;
	/** 店舗検索（市区町村検索）ヘルパー */
    protected $_selectCity_helper = null;
    /** 店舗一覧ヘルパー */
    protected $_list_helper = null;
    /** 店舗詳細ヘルパー */
    protected $_detail_helper = null;
    /** 店舗情報(タイトル)ヘルパー */
    protected $_shoptitle_helper = null;
    /** 検索フォームヘルパー */
    protected $_searchform_helper = null;
    /** twitterヘルパー */
    protected $_twitter_helper = null;
    /** この店舗の携帯ページヘルパー */
    protected $_mobilepage_helper = null;
    /** アパログヘルパー */
    protected $_apalog_helper = null;
    /** 無料電話番号一覧ヘルパー */
    protected $_tellist_helper = null;
    /** 通常電話番号一覧ヘルパー */
    protected $_tellist2_helper = null;

    /** キャンペーン用加盟店ページヘッダーヘルパー */
    protected $_pageheader_helper = null;
    /** キャンペーン用加盟店ページフッターヘルパー */
    protected $_pagefooter_helper = null;

	protected $_suggest_leftpanel_helper   = null;     //2013/02/04：サジェストリンク
	protected $_shinchaku_leftpanel_helper = null;     //左ペイン追加 2013/02/14：新着リンク
	
	/** リニューアルFLG */
	protected $_newTemplate = false;

    /**
     * 初期処理
     */
    public function preDispatch()
    {
		$this->_display_path = '';
    	$this->_main_helper = '';
    	$this->_block = array();
        $this->_baseurl       = $this->_config['apm']['baseurl'];
        $this->_aplbaseurl    = $this->_config['apm']['apl']['static']['baseurl'];
		//$this->_imageurl      = $this->_config['apm']['apl']['image']['baseurl'];
		$this->_imagepath      = $this->_config['apm']['apl']['image']['basepath'];		// 文字列の有無のみチェック
		//$this->_tenpoimageurl = $this->_imageurl . 'tenpoimg/';
		$this->_apalogurl     = $this->_config['apm']['apl']['apalog']['baseurl'];		// 文字列の有無のみチェック
        $this->_googleMapKey  = $this->_config['googlemap']['key'];
		$this->_tenpoimageurl = $this->_config['apm']['apl']['imagesvurl'] . 'fs/tenpoimg/';

    	$shop_helper_path    = APPLICATION_PATH . '/modules/shop/views/helpers/';
        $tellist_helper_path = APPLICATION_PATH . '/modules/shop/views/tellist/helpers/';

        // エラーページ
    	$this->_error_helper                = $this->getApmHelper($shop_helper_path . 'error_helper.tpl');

        // 店舗トップ用
    	$this->_menu_helper                 = $this->getApmHelper($shop_helper_path . 'index/' . 'menu_helper.tpl');
        $this->_newopen_helper              = $this->getApmHelper($shop_helper_path . 'index/' . 'newopen_helper.tpl');
    	// 店舗検索用
        $this->_selectPref_helper           = $this->getApmHelper($shop_helper_path . 'search/' . 'select-pref_helper.tpl');
		$this->_selectRail_helper           = $this->getApmHelper($shop_helper_path . 'search/' . 'select-rail_helper.tpl');
        $this->_selectCity_helper           = $this->getApmHelper($shop_helper_path . 'search/' . 'select-city_helper.tpl');
    	// 店舗一覧用
        $this->_list_helper                 = $this->getApmHelper($shop_helper_path . 'list/' . 'list_helper.tpl');
    	// 店舗詳細用
        $this->_detail_helper               = $this->getApmHelper($shop_helper_path . 'detail/' . 'detail_helper.tpl');
    	$this->_shoptitle_helper            = $this->getApmHelper($shop_helper_path . 'detail/' . 'shoptitle_helper.tpl');
        $this->_searchform_helper           = $this->getApmHelper($shop_helper_path . 'detail/' . 'searchform_helper.tpl');
        $this->_twitter_helper              = $this->getApmHelper($shop_helper_path . 'detail/' . 'twitter_helper.tpl');
        $this->_mobilepage_helper           = $this->getApmHelper($shop_helper_path . 'detail/' . 'mobilepage_helper.tpl');
        $this->_apalog_helper               = $this->getApmHelper($shop_helper_path . 'detail/' . 'apalog_helper.tpl');
    	// 電話番号一覧用
        $this->_tellist_helper              = $this->getApmHelper($tellist_helper_path . 'tellist_helper.tpl');
        $this->_tellist2_helper             = $this->getApmHelper($tellist_helper_path . 'tellist2_helper.tpl');
        $this->_tellistMenu_helper          = $this->getApmHelper($tellist_helper_path . 'menu_helper.tpl');
        $this->_tenpoResult_helper          = $this->getApmHelper($tellist_helper_path . 'tenpo-result_helper.tpl');

    	// キャンペーン用加盟店ページ詳細用		/* 2011/07/22　AKBｷｬﾝﾍﾟｰﾝ：加盟店ﾒﾝﾊﾞｰｻｲﾄ追加対応 */
        $this->_pageheader_helper     		= $this->getApmHelper($shop_helper_path . 'shoppage/' . 'pageheader_helper.tpl');
        $this->_pagefooter_helper     		= $this->getApmHelper($shop_helper_path . 'shoppage/' . 'pagefooter_helper.tpl');

        $this->_data['menu_index_helper']   = $shop_helper_path . 'index/' . 'menu_index_helper.tpl';
        $this->_data['menu_city_helper']    = $shop_helper_path . 'index/' . 'menu_city_helper.tpl';
        $this->_data['menu_rail_helper']    = $shop_helper_path . 'index/' . 'menu_rail_helper.tpl';
        $this->_data['common_select_pref_helper'] = $shop_helper_path . 'common_select_pref_helper.tpl';

		//** 2013/02/04：サジェストリンクヘルパー追加
		$this->_suggest_leftpanel_helper  = $this->getApmHelper('search/views/helpers/suggestLink.tpl');  //サジェストリンク追加
    	//2013/02/14 左ペイン追加新着リンク
        $this->_shinchaku_leftpanel_helper  = $this->getApmHelper('search/views/helpers/shinchakuLink.tpl');  
 
        // パラメータの取得
        $this->_params = $this->getParameters();
        // パラメータをフィルタリング
        $this->filteringParams();
        // 共通CMS用パラメータ設定
        $this->setCommonCmsParams();

		// Lorder
		$loader = new Zend_Application_Module_Autoloader(array(
			'namespace' => 'Search',
			'basePath' => APPLICATION_PATH . '/modules/search',
		));

    }

    /**
     * 後処理
     */
    public function postDispatch()
    {
        // hiddenタグ生成
        $this->_data['hiddenTag'] = Apm_Util::generateHiddenTag($this->_params);
        // リクエストデータ設定
        $this->_data['request'] = $this->_params;
        // リクエストパラメータ変換
        $model = new Shop_Model_Base();
        $model->convertRequestParam($this->_data['request']);
        // URL設定
        $this->setUrlParams();

		if ($this->_newTemplate) {
			$shop_helper_path = APPLICATION_PATH . '/modules/shop/views/helpers/';
			
			$this->_menu_helper       = $this->getApmHelper($shop_helper_path . 'index/'  . 'menu_new_helper.tpl');
			$this->_searchform_helper = $this->getApmHelper($shop_helper_path . 'detail/' . 'searchform_new_helper.tpl');
			$this->_twitter_helper    = $this->getApmHelper($shop_helper_path . 'detail/' . 'twitter_new_helper.tpl');
			$this->_mobilepage_helper = $this->getApmHelper($shop_helper_path . 'detail/' . 'mobilepage_new_helper.tpl');
			$this->_apalog_helper     = $this->getApmHelper($shop_helper_path . 'detail/' . 'apalog_new_helper.tpl');
			
			$this->_data['menu_index_helper'] = $shop_helper_path . 'index/' . 'menu_index_new_helper.tpl';
			$this->_data['menu_city_helper']  = $shop_helper_path . 'index/' . 'menu_city_new_helper.tpl';
			$this->_data['menu_rail_helper']  = $shop_helper_path . 'index/' . 'menu_rail_new_helper.tpl';
			$this->_data['common_select_pref_helper'] = $shop_helper_path . 'common_select_pref_new_helper.tpl';
			
			if (!empty($this->_params['PrefCd'][0]) && count($this->_params['PrefCd']) == 1) {
				// ランキングリンクブロック生成
				// 初期化
				$rank_data = array();
				// テンプレート
				$ranking_block = $this->getApmHelper('search/views/helpers/seo_ranking_leftpanel.tpl');
				// ** こだわり取得 **
				$rank_data['RANK_KODAWARI'] = Apm_Seo::getSeoRankingKodawari(5, 8, $this->_params['PrefCd'][0]);
				// ** 建物種別取得 **
				$rank_data['RANK_TYPE']     = Apm_Seo::getSeoRankingType($this->_params);
				// ブロック生成
				$this->_data['ranking_block'] = $ranking_block->getBlock($rank_data);
				
				// 2013/02/04 サジェストリンク追加対応 ss-sakakibaraw
				$this->_data['suggestBlockData'] = Apm_Seo::getSuggestLinkBlockData($this->_params['PrefCd'][0],15,5,5,15);
				$this->_data['suggest_block'] = $this->_suggest_leftpanel_helper->getBlock($this->_data);
				
				//2013/02/14 新着リンク追加対応
				$this->_data['shinchakuBlockData'] = Apm_Seo::getTodoufukenShinchakuBlockData($this->_params['PrefCd'][0],0);
				$this->_data['shinchaku_block'] = $this->_shinchaku_leftpanel_helper->getBlock($this->_data);
			}
		}
		// SEO検索ブロックの挿入
		if ($this->_import_seo_search) {
			$this->_data['seo_search_data']['PREF'] = Apm_Seo::getRandomPref(5);
			$this->_data['seo_search_data']['KODAWARI'] = Apm_Seo::getSeoImpKodawari(3,3);
			$this->_data['seo_search_tpl'] = APPLICATION_PATH . '/modules/common/views/scripts/seo_search.tpl';
		}

        if (is_string($this->_main_helper))
        {
	    	$this->_block['main'] = $this->_main_helper;
        }
        else
        {
	    	$this->_block['main'] = $this->_main_helper->getBlock($this->_data);
        }

        // 店舗左メニュー
    	$this->_block['menu'] = $this->_menu_helper->getBlock($this->_data);
    	// 電話番号一覧左メニュー
    	$this->_block['tellistMenu'] = $this->_tellistMenu_helper->getBlock($this->_data);
    	// 店舗詳細
        $this->_block['shoptitle']   = $this->_shoptitle_helper->getBlock($this->_data);
        $this->_block['searchform']  = $this->_searchform_helper->getBlock($this->_data);
        $this->_block['twitter']     = $this->_twitter_helper->getBlock($this->_data);
        $this->_block['mobilepage']  = $this->_mobilepage_helper->getBlock($this->_data);
        $this->_block['apalog']      = $this->_apalog_helper->getBlock($this->_data);

    	// キャンペーン用加盟店ページ詳細用
        $this->_block['pageheader']   = $this->_pageheader_helper->getBlock($this->_data);
        $this->_block['pagefooter']   = $this->_pagefooter_helper->getBlock($this->_data);

    	// 近隣店舗リスト用
        $this->_block['kinrinlist']   = $this->_list_helper->getBlock($this->_data);

		// 都道府県別キャンペーン対応
		if (!(array_key_exists('isMobile', $this->_params) && $this->_params['isMobile'] == 1)					// APIでない
			&& !((array_key_exists('OemCd', $this->_params)) && (is_null($this->_params['OemCd']) == false))	// OEMでない
			&& $this->_params['SrhKbn'] != Apm_Config_Search::SRHKBN_SYATAKU									// 社宅でない
			&& (array_key_exists('PrefCd', $this->_params))
		   )
		{
			if (is_array($this->_params['PrefCd'])) {
				foreach ($this->_params['PrefCd'] as $PrefCd) {
					if ($PrefCd >= '50' and $PrefCd <= '53') $PrefCd = '01';
					$this->_data['bnr_pref_'.$PrefCd] = 'on';
				}
			} else {
				$this->_data['bnr_pref_'.$this->_params['PrefCd']] = 'on';
			}
		}

        // Block設定
    	$this->_data['block'] = $this->_block;
    	// パンくず作成
        $this->setBreadCrumb($this->_breadcrumb->generateBreadcrumb());
    	// 表示
        $this->display($this->_display_path);
    }

    /**
     * リクエストパラメータの値取得
     * @param 連想配列のキー
     */
    protected function getParamValue($key)
    {
       	return (array_key_exists($key, $this->_params)) ? $this->_params[$key] : null;
    }

    /**
     * urlに記述する引数を取得
     *
     * @param $keyList
     * @return 引数
     */
    protected function createUrlParams()
    {
    	return $this->getUrlParams(array_keys($this->_params));
    }

    /**
     * urlに記述する引数を作成(店舗詳細リンク用)
     *
     * @param $keyList
     * @return 引数
     */
    protected function createUrlParamsForDetail()
    {
    	$keyList = array();
    	$keyList[] = 'PlaceKbn';
    	$keyList[] = 'SrhKbn';
    	$keyList[] = 'OemCd';
    	$keyList[] = 'AreaCd';
    	$keyList[] = 'SrhType';

    	return $this->getUrlParams($keyList);
    }

    /**
     * 検索区分チェック
     */
    protected function checkSrhKbn()
    {
    	$srhKbn = $this->getParamValue('SrhKbn');

    	if ($srhKbn != Apm_Config_Shop::SEARCH_KUBUN_MAP &&
    		$srhKbn != Apm_Config_Shop::SEARCH_KUBUN_FREEWORD &&
    		$srhKbn != Apm_Config_Shop::SEARCH_KUBUN_HOUZIN &&
    		$srhKbn != Apm_Config_Shop::SEARCH_KUBUN_OTHER)
    	{
    		return false;
    	}
       	return true;
    }

    /**
     * 直近のパンくずを更新
     */
    protected function updatePankuzu()
    {
        $count = $this->_breadcrumb->getCountPankuzu();
        if ($count > 0)
        {
        	$index = $count - 1;
        	$pankuzu = $this->_breadcrumb->getDataByIndex($index);
        	$params = strstr($pankuzu['url'], '?');
        	$url = str_replace($params, '', $pankuzu['url']);

        	$pankuzu['url'] = $url . $this->createUrlParams();
        	$this->_breadcrumb->setDataByIndex($index, $pankuzu);
        }
    }

    /**
     * URLの先頭にスラッシュがある場合は、先頭のスラッシュを削除する
     *
     * @param $url
     */
    protected function deleteDiagonal($url)
    {
    	$value = $url;
    	$diagonal = substr($url, 0, 1);

    	if ($diagonal == '/')
    	{
    		$value = substr($url, 1);
    	}
    	return $value;
    }

    /**
     * urlに記述する引数を取得
     */
    private function getUrlParams($keyList)
    {
    	foreach ($keyList as $key)
    	{
    		if ($key == 'backurl') continue;

    		$value = $this->getParamValue($key);

    		if (is_array($value))
    		{
    			foreach ($value as $val)
    			{
    				$paramList[] = $key . '[]=' . urlencode(mb_convert_encoding($val, 'UTF-8', 'SJIS'));
    			}
    		}
    		else
    		{
    			$paramList[] = $key . '=' . urlencode(mb_convert_encoding($value, 'UTF-8', 'SJIS'));
    		}
    	}

    	$strParam = '?' . join('&', $paramList);
    	return $strParam;
    }

    /**
     * パラメータをフィルタリング
     */
    private function filteringParams()
    {
        // 配列をフィルタリング
        $this->arrayFiltering();

        // サービス内容から検索の場合は、賃貸斡旋をチェック済みにする
        if (array_key_exists('tokucho01', $this->_params) == false)
        {
        	$srhKbn = $this->getParamValue('SrhKbn');

        	if (!is_null($srhKbn) && $srhKbn == Apm_Config_Shop::SEARCH_KUBUN_MAP)
        	{
	        	$this->_params['tokucho01'] = 1;
        	}
        }

        $srhType = $this->getParamValue('SrhType');
        if (empty($srhType))
        {
	        // 初回は住所から探すモード
	        $this->_params['SrhType'] = Apm_Config_Shop::SEARCH_TYPE_CITY;
        }
    }

    /**
     * 配列フィルタリング
     */
    private function arrayFiltering()
    {
    	foreach($this->_params as $key => $value)
    	{
    		if (is_array($this->_params[$key]))
    		{
	        	$this->_params[$key] = array_filter($this->_params[$key]);
	        	$this->_params[$key] = array_unique($this->_params[$key]);
    		}
    	}
    }

    /**
     * URL設定
     */
    private function setUrlParams()
    {
    	$url = array();

    	$url['tellist_index']  = Apm_Config_Shop::URL_TELLIST_INDEX;
    	$url['tellist_index2'] = Apm_Config_Shop::URL_TELLIST_INDEX2;
    	$url['index']          = Apm_Config_Shop::URL_INDEX;
    	$url['select_pref']    = Apm_Config_Shop::URL_SELECT_PREF;
    	$url['select_city']    = Apm_Config_Shop::URL_SELECT_CITY;
    	$url['select_rail']    = Apm_Config_Shop::URL_SELECT_RAIL;
    	$url['list']           = Apm_Config_Shop::URL_LIST;
    	$url['detail']         = Apm_Config_Shop::URL_DETAIL;
    	$url['map']            = Apm_Config_Shop::URL_MAP;
    	$url['tenpo_oem_jump'] = Apm_Config_Shop::URL_TENPO_OEM_JUMP;
    	$url['sendmail']       = Apm_Config_Shop::URL_SEND_MAIL;

       	$this->_data['url'] = $url;
    }

    /**
     * 共通CMS用パラメータ設定
     */
    public function setCommonCmsParams()
    {
        // アンカー設定
        $this->setAnchorLink2();
        //$this->setAnchorLink('<p class="pagetop"><a href="javascript:backToTop()"><strong>ページトップへ</strong></a></p>');
    }
}
?>
